/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import beans.Boliche;
import beans.Persona;
import beans.Socio;
import beans.Visita;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.ComparatorPorPuntaje;

/**
 *
 * @author nMoncho
 */
public class PersonaDao {

    private static Map<String, Socio> socios = new HashMap<String, Socio>();

    public static Persona getPersonaPorID(Integer id) {
        Persona retorno = null;
        SessionFactory sf = SimpleSessionFactory.getSessionFactory(null);
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from beans.Persona where id = :id");
        q.setInteger("id", id);

        retorno = (Persona)q.uniqueResult();
        
        session.close();

        return retorno;
    }

    public static Socio getSocioPorLegajo(String legajo){
        Socio retorno = null;
        SessionFactory sf = SimpleSessionFactory.getSessionFactory(null);
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from beans.Socio socio where socio.legajo = :legajo");
        q.setString("legajo", legajo);
        List result = q.list();
        for (Object obj : result) {
            retorno = (Socio) obj;
        }

        session.close();
        //socios.put(legajo, retorno);
        
        return retorno;
    }


    public static Socio getSocioPorLegajoEIDBoliche(String legajo, Integer id) {
        Socio retorno = null;
        SessionFactory sf = SimpleSessionFactory.getSessionFactory(null);
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from beans.Socio socio where socio.legajo = :legajo and socio.boliche.id = :id");
        q.setString("legajo", legajo);
        q.setInteger("id", id);
        
        retorno = (Socio) q.uniqueResult();
        
        session.close();
        
        return retorno;
    }

    public static List<Socio> getSociosOrdenadosPorPuntaje(){
        SessionFactory sf = SimpleSessionFactory.getSessionFactory(null);
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from Socio");
        List result = q.list();

        Collections.sort(result, new ComparatorPorPuntaje());

        session.close();

        return result;
    }

    public static boolean saveOrUpdate(Socio socio){
        SessionFactory sf = null;
        Session session = null;
        try{
            sf = SimpleSessionFactory.getSessionFactory(null);
            session = sf.openSession();
            session.beginTransaction();

            session.saveOrUpdate(socio);
            session.getTransaction().commit();

            return true;//Exito!
        }catch(Exception ex){
            if(session != null && session.isOpen())
                session.getTransaction().rollback();

            return false;//Fallido
        }finally{
            session.close();
        }
    }

    public static boolean saveOrUpdate(List<Socio> socios){
        SessionFactory sf = null;
        Session session = null;
        try{
            sf = SimpleSessionFactory.getSessionFactory(null);
            session = sf.openSession();
            session.beginTransaction();

            for(Socio socio : socios){
                session.saveOrUpdate(socio);
            }

            session.getTransaction().commit();

            return true;//Exito
        }catch(Exception ex){
            if(session != null && session.isOpen())
                session.getTransaction().rollback();

            ex.printStackTrace();
            return false;//Fallo
        }finally{
            session.close();
        }
    }

    public static boolean saveOrUpdate(Persona persona){
        SessionFactory sf = null;
        Session session = null;
        try{
            sf = SimpleSessionFactory.getSessionFactory(null);
            session = sf.openSession();
            session.beginTransaction();

            session.saveOrUpdate(persona);
            session.getTransaction().commit();

            return true;//exito!
        }catch(Exception ex){
            if(session != null && session.isOpen())
                session.getTransaction().rollback();

            return false;//fallo
        }finally{
            session.close();
        }
    }

    public static boolean saveVisitas(List<Socio> socios, Visita[] visitas){
        SessionFactory sf = null;
        Session session = null;
        try{
            sf = SimpleSessionFactory.getSessionFactory(null);
            session = sf.openSession();
            session.beginTransaction();
            int i=0;
            Socio socioMerge;
            for(Socio socio : socios){
                System.out.println("Socio MERGE!!!!");
                socioMerge = (Socio)session.merge(socio);
                socioMerge.getVisitas().add(visitas[i]);
                i++;
            }

            session.getTransaction().commit();
            session.flush();
            
            return true;//Exito
        }catch(Exception ex){
            if(session != null && session.isOpen())
                session.getTransaction().rollback();

            ex.printStackTrace();
            return false;//Fallo
        }finally{
            session.close();
        }
    }

    public static List<Socio> getSocios(){
        List<Socio> retorno = new LinkedList<Socio>();
        SessionFactory sf = SimpleSessionFactory.getSessionFactory(null);
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from beans.Socio");

        List result = q.list();
        retorno.addAll(result);

        session.close();

        return retorno;
    }

    public static Socio getSocioPorLegajoYBoliche(String legajo, Boliche boliche) {
        Socio retorno = socios.get(legajo);
        if (retorno == null) {
            SessionFactory sf = SimpleSessionFactory.getSessionFactory(null);
            Session session = sf.openSession();
            session.beginTransaction();
            Query q = session.createQuery("from Socio soc where soc.legajo = :legajo and soc.boliche.id = :boliche");
            q.setString("legajo", legajo);
            q.setInteger("boliche", boliche.getId());
            List result = q.list();
            for (Object obj : result) {
                retorno = (Socio) obj;
            }

            session.close();
            socios.put(legajo, retorno);
        }

        return retorno;
    }

    public static boolean existeLegajoParaBoliche(String legajo, Boliche boliche) {
        SessionFactory sf = null;
        Session session = null;
        try{
            sf = SimpleSessionFactory.getSessionFactory(null);
            session = sf.openSession();
            Query q = session.createQuery("select count(*) from beans.Socio socio\n "
                    + " where socio.legajo = :legajo\n and socio.boliche.id = :idBoliche");
            q.setString("legajo", legajo);
            q.setInteger("idBoliche", boliche.getId().intValue());

            Long count = (Long)q.uniqueResult();

            if(count.intValue() == 0)
                return false;
            else
                return true;
            
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            session.close();
        }

        return true;//por defecto
    }

    public static boolean existeLegajoParaBoliche(Socio socio, String legajo) {
        SessionFactory sf = null;
        Session session = null;
        try{
            sf = SimpleSessionFactory.getSessionFactory(null);
            session = sf.openSession();
            Query q = session.createQuery("select count(*) from beans.Socio socio\n "
                    + " where socio.legajo = :legajo\n and socio.boliche.id = :idBoliche");
            q.setString("legajo", legajo);
            q.setInteger("idBoliche", socio.getBoliche().getId().intValue());

            Long count = (Long)q.uniqueResult();

            if(count.intValue() == 0)
                return false;
            else
                return true;

        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            session.close();
        }

        return true;//por defecto
    }

    public static boolean saveOrUpdateAndDelete(Socio socio, List<Persona> amigosABorrar) {
        SessionFactory sf = null;
        Session session = null;
        try{
            sf = SimpleSessionFactory.getSessionFactory(null);
            session = sf.openSession();
            session.beginTransaction();

            for(Persona amigo : amigosABorrar)
                session.delete(amigo);

            session.saveOrUpdate(socio);
            session.getTransaction().commit();

            return true;//Exito!
        }catch(Exception ex){
            if(session != null && session.isOpen())
                session.getTransaction().rollback();

            return false;//Fallido
        }finally{
            session.close();
        }
    }

}
